{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date.resolution: 1 day, 0:00:00\n",
      "time.resolution: 0:00:00.000001\n",
      "datetime.resolution: 0:00:00.000001\n",
      "date.max: 9999-12-31 and date.min: 0001-01-01\n",
      "time.max: 23:59:59.999999 and time.min: 00:00:00\n",
      "datetime.max: 9999-12-31 23:59:59.999999 and datetime.min: 0001-01-01 00:00:00\n",
      "datetime: 2016-10-26 10:23:15.000001\n",
      "datetime: 2016-10-27 20:23:15.000001\n",
      "datetime.isoformat(): 2016-10-26T10:23:15.000001\n",
      "datetime.strptime():2016-10-26 00:00:00\n",
      "fromtimestamp():2015-04-19 12:20:00\n",
      "utcfromtimestamp():2015-04-19 04:20:00\n",
      "datetime.now():2025-04-25 11:12:14.692250\n",
      "<class 'datetime.timedelta'> 17 days, 0:00:00\n",
      "17 1468800.0\n",
      "2025-04-26 12:12:14.692766\n",
      "2025-04-24 11:12:14.692766\n",
      "2025-04-24 11:12:14.692766\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Users\\yibozhang\\AppData\\Local\\Temp\\ipykernel_34792\\1240336844.py:28: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).\n",
      "  print(f'utcfromtimestamp():{datetime.utcfromtimestamp(1429417200.0)}')\n"
     ]
    }
   ],
   "source": [
    "from datetime import date, time, datetime , timedelta\n",
    "# date.resolution; date对象表示日期的最小单位\n",
    "print(f'date.resolution: {date.resolution}')\n",
    "# time.resolution: time对象表示时间的最小单位\n",
    "print(f'time.resolution: {time.resolution}')\n",
    "# datetime.resolution: datetime对象表示时间的最小单位\n",
    "print(f'datetime.resolution: {datetime.resolution}')\n",
    "# date.max，date.min;date对象所能表示的最大，最小日期范围\n",
    "print(f'date.max: {date.max} and date.min: {date.min}')\n",
    "# time.max, time.min；time对象所能表示的最大，最小时间范围\n",
    "print(f'time.max: {time.max} and time.min: {time.min}')\n",
    "# datetime.max,datetime.min: datetime对象所能表示的最大和最小时间范围\n",
    "print(f'datetime.max: {datetime.max} and datetime.min: {datetime.min}')\n",
    "\n",
    "#构造datetime实例对象\n",
    "datetime_obj = datetime(2016,10,26,10,23,15,1)\n",
    "print(f'datetime: {datetime_obj}')\n",
    "#replace用参数指定代替原有对象中的属性生成新的datetime时间对象\n",
    "re_datetime_obj = datetime_obj.replace(day = 27, hour = 20)\n",
    "print(f'datetime: {re_datetime_obj}')\n",
    "#.isformat(),返回型如yyyy-MM-DD HH:MM:SS格式的字符串时间\n",
    "print(f'datetime.isoformat(): {datetime_obj.isoformat()}')\n",
    "#strptime将字符串转换为datetime对象\n",
    "print(f'datetime.strptime():{datetime.strptime(\"2016-10-26\", \"%Y-%m-%d\")}')\n",
    "#fromtimestamp根据时间戳创建一个datetime\n",
    "print(f'fromtimestamp():{datetime.fromtimestamp(1429417200.0)}')\n",
    "#utcfromtimestamp根据时间戳创建一个格林威治时间的datetime\n",
    "print(f'utcfromtimestamp():{datetime.utcfromtimestamp(1429417200.0)}')\n",
    "#datetime.now生成一个当前本地时间的datetime\n",
    "print(f'datetime.now():{datetime.now()}')\n",
    "#二个datetime对象直接相减能获得一个timedelta对象\n",
    "date1 = datetime.strptime(\"2019-10-18 04:20:00\", \"%Y-%m-%d %X\")\n",
    "date2 = datetime.strptime(\"2019-10-01 04:20:00\", \"%Y-%m-%d %X\")\n",
    "delta_obj =date1 - date2\n",
    "print(type(delta_obj), delta_obj)\n",
    "print(delta_obj.days, delta_obj.total_seconds())\n",
    "# 时间加减\n",
    "dt = datetime.now()\n",
    "# 明天后1小时\n",
    "dt1 = dt + timedelta(days=1, hours = 1)\n",
    "# 昨天\n",
    "dt2 = dt + timedelta(days=-1)\n",
    "# 昨天\n",
    "dt3 = dt - timedelta(days=1)\n",
    "print(f\"{dt1}\\n{dt2}\\n{dt3}\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
